package com.zmj.service.impl;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;

/**
 * @author Xuzhenyang
 * @since Created in 2021/1/14 11:22
 */
public class HtmlToPdfService {

    private static final String htmlPath = "C:\\Users\\yangquan\\Desktop\\book\\pdfOli\\00794420210108190032.html";
    private static final String outputPath = "C:\\Users\\yangquan\\Desktop\\book\\pdfOli\\1.pdf";

    /**
     * 生成PDF文件的主要方法
     *
     * @param filePath 生成pdf文件存放的完整绝对路径  example: /test.pdf
     * @param content pdf 正文
     * @param waterMark pdf 每页的水印， 建议不要过长 8个字符差不多了， 太长需要调节字体大小， 否则无法显示完整
     */
    public static void createPDF(String filePath, String content, String waterMark) {
        // Assert.notNull(filePath, "FilePath can not be null");
        // Assert.notNull(content, "Content can not be null");
        // Assert.notNull(waterMark, "WaterMark can not be null");
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
            writer.setPageEvent(new CustomEvent(waterMark));
            document.addTitle("PDF标题");
            document.open();

            // 额外添加内容的方法 如果只是普通文本转pdf, 用下面注释的代码即可, 注释后面的代码为html to pdf
            // BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
            // Font font = new Font(baseFont);
            // document.add(new Paragraph(content, font));

            ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
            ByteArrayInputStream is = new ByteArrayInputStream("".getBytes());
            XMLWorkerHelper.getInstance().parseXHtml(writer, document, in, is, new PdfFont());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }

    /**
     * 中文字体支持
     */
    static class PdfFont extends XMLWorkerFontProvider {

        @Override
        public Font getFont(String fontname, String encoding, boolean embedded, float size, int style,
            BaseColor color) {
            String font = fontname;
            if (font == null) {
                font = "宋体";
            }
            if (size <= 0) {//size<0 会使内容重叠在一体
                size = 10.5f;
            }
            return super.getFont(font, encoding, embedded, size, style, color);
        }

    }

    /**
     * 事件处理类, 用于监听pdf页码增加时, 每页增加水印
     */
    static class CustomEvent extends PdfPageEventHelper {

        private String waterMark;

        public CustomEvent(String waterMark) {
            this.waterMark = waterMark;
        }

        @Override
        public void onStartPage(PdfWriter writer, Document document) {
            try {
                // 加入水印
                PdfContentByte waterMar = writer.getDirectContentUnder();
                // 开始设置水印
                waterMar.beginText();
                // 设置水印透明度
                PdfGState gs = new PdfGState();
                // 设置填充字体不透明度为0.2f
                gs.setFillOpacity(0.1f);
                // 设置水印字体参数及大小
                BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
                waterMar.setFontAndSize(baseFont, 60);
                // 设置透明度
                waterMar.setGState(gs);
                // 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
                waterMar.showTextAligned(Element.ALIGN_CENTER, waterMark, 300, 500, 45);
                //结束设置
                waterMar.endText();
                waterMar.stroke();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        String content = "<!DOCTYPE html>\n" + "<html lang=\"en\">\n" + "\n" + "\t<head>\n"
            + "\t\t<meta charset=\"UTF-8\"/>\n" + "\t\t <style type=\"text/css\">\n"
            + "\t\t\t/*解决html转pdf文件中文不显示的问题*/\n" + "\t\t\tbody {\n" + "\t\t\t\tfont-family: SimSun;\n" + "\t\t\t}\n"
            + "        </style>\n" + "\t\t<title>郑煤机推移千斤顶综合测试平台</title>\n"
            + "\t\t<link rel=\"icon\" href=\"C:\\Users\\yangquan\\Desktop\\book\\pdfOli\\static\\img\\title_icon.ico\" type=\"image/x-icon\"/>\n"
            + "\t\t<link rel=\"shortcut icon\" href=\"C:\\Users\\yangquan\\Desktop\\book\\pdfOli\\static\\img\\title_icon.ico\" type=\"image/x-icon\"/>\n"
            + "\t\t<link rel=\"stylesheet\" href=\"C:\\Users\\yangquan\\Desktop\\book\\pdfOli\\static\\index.css\"/>\n"
            + "\t\t<!-- <link rel=\"icon\" href=\"static/img/title_icon.ico\" type=\"image/x-icon\"/>\n"
            + "\t\t<link rel=\"shortcut icon\" href=\"static/img/title_icon.ico\" type=\"image/x-icon\"/>\n"
            + "\t\t<link rel=\"stylesheet\" href=\"static/index.css\"/>-->\n"
            + "\t\t<script src=\"https://cdn.jsdelivr.net/npm/vue/dist/vue.js\"></script>\n" + "\t</head>\n"
            + "\t<body>\n" + "\t\t<div id=\"app\" class=\"content-wraper\">\n" + "\t\t\t<h2 class=\"title\">\n"
            + "\t\t\t\t <img src=\"C:\\Users\\yangquan\\Desktop\\book\\pdfOli\\static\\img\\logoMini.png\"/>\n"
            + "\t\t\t\t<!--<img src=\"static/img/logoMini.png\"/>-->\n" + "\t\t\t\t郑州煤矿机械集团\n" + "\t\t\t</h2>\n"
            + "\t\t\t<h3 class=\"title vice-title\">\n" + "\t\t\t\t油缸检测实验报告\n" + "\t\t\t</h3>\n"
            + "\t\t\t<!-- 摘要 --><div class=\"item-wrap\">\n" + "\t\t\t\t<h5 class=\"vice-min-title\">概要</h5>\n"
            + "\t\t\t\t<table class=\"table\" border=\"0\">\n" + "\t\t\t\t\t<tbody>\n" + "\t\t\t\t\t\t<tr>\n"
            + "\t\t\t\t\t\t\t<td>施工号</td>\n" + "\t\t\t\t\t\t\t<td>Z20-107</td>\n" + "\t\t\t\t\t\t\t<td>油缸编号</td>\n"
            + "\t\t\t\t\t\t\t<td>014</td>\n" + "\t\t\t\t\t\t</tr>\n" + "\t\t\t\t\t\t<tr>\n"
            + "\t\t\t\t\t\t\t<td>类型</td>\n" + "\t\t\t\t\t\t\t<td>平衡千⽄顶</td>\n" + "\t\t\t\t\t\t\t<td>检测结果</td>\n"
            + "\t\t\t\t\t\t\t<td style=\"font-weight: bold;\">成功</td>\n" + "\t\t\t\t\t\t</tr>\n"
            + "\t\t\t\t\t</tbody>\n" + "\t\t\t\t</table>\n" + "\t\t\t</div><!-- 折线图 -->\n"
            + "\t\t\t<div class=\"item-wrap\">\n" + "\t\t\t\t<h5 class=\"vice-min-title\">压力数据</h5>\n"
            + "\t\t\t\t<div class=\"canas-wraper d-flex\">\n" + "\t\t\t\t\t<div class=\"pre-canvas\">\n"
            + "\t\t\t\t\t\t<div id=\"echart-pre-wrap\"></div>\n" + "\t\t\t\t\t</div>\n" + "\t\t\t\t</div>\n"
            + "\t\t\t</div>\n" + "\t\t\t<!-- 结果表格 -->\n" + "\t\t\t<div class=\"item-wrap\">\n"
            + "\t\t\t\t<table class=\"table\" border=\"0\">\n" + "\t\t\t\t\t<tbody id=\"pre-table\">\n"
            + "\t\t\t\t\t\t<tr>\n" + "\t\t\t\t\t\t\t<td>部位</td>\n" + "\t\t\t\t\t\t\t<td>压力值（Mpa）</td>\n"
            + "\t\t\t\t\t\t\t<td>保压时长（s）</td>\n" + "\t\t\t\t\t\t\t<td>压降（Mpa）</td>\n" + "\t\t\t\t\t\t</tr><tr>\n"
            + "    \t\t\t\t\t\t\t<td>活塞杆腔实际保压压力</td>\n" + "    \t\t\t\t\t\t\t<td>47.53</td>\n"
            + "    \t\t\t\t\t\t\t<td>120</td>\n" + "    \t\t\t\t\t\t\t<td>0.12</td>\n" + "    \t\t\t\t\t\t</tr><tr>\n"
            + "    \t\t\t\t\t\t\t<td>活塞腔实际保压压力</td>\n" + "    \t\t\t\t\t\t\t<td>48.85</td>\n"
            + "    \t\t\t\t\t\t\t<td>120</td>\n" + "    \t\t\t\t\t\t\t<td>0.12</td>\n"
            + "    \t\t\t\t\t\t</tr></tbody>\n" + "\t\t\t\t</table>\n" + "\t\t\t</div><div class=\"tip\">\n"
            + "\t\t\t\t<p>\n" + "\t\t\t\t\t<span>检测员：</span><span>007944</span>\n" + "\t\t\t\t</p>\n" + "\t\t\t\t<p>\n"
            + "\t\t\t\t\t<span>实验时间：</span><span>2021-01-08 19:00:32</span>\n" + "\t\t\t\t</p>\n"
            + "\t\t\t</div></div>\n" + "\n"
            + "\t\t<script src=\"C:\\Users\\yangquan\\Desktop\\book\\pdfOli\\static/jquery.min.js\"></script>\n"
            + "\t\t<script src=\"C:\\Users\\yangquan\\Desktop\\book\\pdfOli\\static/echarts.min@4.9.0.js\"></script>\n"
            + "\t\t<script type=\"text/javascript\">\n" + "\t\t\t\n" + "\n" + "\t\t\tfunction echartOption(data) {\n"
            + "\t\t\t\tconst option = {\n" + "\t\t\t\t\tgrid: {\n" + "\t\t\t\t\t\tleft: '4%',\n"
            + "\t\t\t\t\t\tright: '5%',\n" + "\t\t\t\t\t\ttop: '6%',\n" + "\t\t\t\t\t\tbottom: '8%'\n"
            + "\t\t\t\t\t},\n" + "\t\t\t\t\txAxis: {\n" + "\t\t\t\t\t\ttype: 'category',\n"
            + "\t\t\t\t\t\tdata: data.currentTime,\n" + "\t\t\t\t\t},\n" + "\t\t\t\t\tyAxis: {\n"
            + "\t\t\t\t\t\ttype: 'value',\n" + "\t\t\t\t\t\tmin: 0,\n" + "\t\t\t\t\t\tmax: 80,\n"
            + "\t\t\t\t\t\tsplitNumber: 11\n" + "\t\t\t\t\t},\n" + "\t\t\t\t\tseries: [{\n"
            + "\t\t\t\t\t\t\tsymbol: 'none',\n" + "\t\t\t\t\t\t\tsmooth: true,\n" + "\t\t\t\t\t\t\tdata: data.upPre,\n"
            + "\t\t\t\t\t\t\ttype: 'line'\n" + "\t\t\t\t\t\t},\n" + "\t\t\t\t\t\t{\n"
            + "\t\t\t\t\t\t\tsymbol: 'none',\n" + "\t\t\t\t\t\t\tsmooth: true,\n"
            + "\t\t\t\t\t\t\tdata: data.downPre,\n" + "\t\t\t\t\t\t\ttype: 'line'\n" + "\t\t\t\t\t\t},\n"
            + "\t\t\t\t\t]\n" + "\t\t\t\t}\n" + "\t\t\t\treturn option\n" + "\t\t\t}\n" + "\t\t\tnew Vue({\n"
            + "\t\t\t\tel: '#app',\n" + "\t\t\t\tdata: {\n" + "\t\t\t\t\tshiGongHao: 200\n" + "\t\t\t\t},\n"
            + "\t\t\t\tmounted() {\n"
            + "\t\t\t\t\tlet preChart = echarts.init(document.getElementById('echart-pre-wrap'));\n"
            + "\t\t\t\t\tlet options = echartOption(\n" + "\t\t\t\t\t\t{\n"
            + "\t\t\t\t\t\t\tcurrentTime:[0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5, 15.0, 16.5, 18.0, 19.5, 21.0, 22.5, 24.0, 25.5, 27.0, 28.5, 30.0, 31.5, 33.0, 34.5, 36.0, 37.5, 39.0, 40.5, 42.0, 43.5, 45.0, 46.5, 48.0, 49.5, 51.0, 52.5, 54.0, 55.5, 57.0, 58.5, 60.0, 61.5, 63.0, 64.5, 66.0, 67.5, 69.0, 70.5, 72.0, 73.5, 75.0, 76.5, 78.0, 79.5, 81.0, 82.5, 84.0, 85.5, 87.0, 88.5, 90.0, 91.5, 93.0, 94.5, 96.0, 97.5, 99.0, 100.5, 102.0, 103.5, 105.0, 106.5, 108.0, 109.5, 111.0, 112.5, 114.0, 115.5, 117.0, 118.5, 120.0, 121.5, 123.0, 124.5, 126.0, 127.5, 129.0, 130.5, 132.0, 133.5, 135.0, 136.5, 138.0, 139.5, 141.0, 142.5, 144.0, 145.5, 147.0, 148.5, 150.0, 151.5, 153.0, 154.5, 156.0, 157.5, 159.0, 160.5, 162.0, 163.5, 165.0, 166.5, 168.0, 169.5, 171.0, 172.5, 174.0, 175.5, 177.0, 178.5, 180.0, 181.5, 183.0, 184.5, 186.0, 187.5, 189.0, 190.5, 192.0, 193.5, 195.0, 196.5, 198.0, 199.5, 201.0, 202.5, 204.0, 205.5, 207.0, 208.5, 210.0, 211.5, 213.0, 214.5, 216.0, 217.5, 219.0, 220.5, 222.0, 223.5, 225.0, 226.5, 228.0, 229.5, 231.0, 232.5, 234.0, 235.5, 237.0, 238.5, 240.0, 241.5, 243.0, 244.5, 246.0, 247.5, 249.0, 250.5, 252.0, 253.5, 255.0, 256.5, 258.0, 259.5, 261.0, 262.5, 264.0, 265.5, 267.0, 268.5, 270.0, 271.5, 273.0, 274.5, 276.0, 277.5, 279.0, 280.5, 282.0, 283.5, 285.0, 286.5, 288.0, 289.5, 291.0],\n"
            + "\t\t\t\t\t\t\tupPre:[-0.01, 0.00, 1.25, 2.60, 0.04, 0.12, 0.17, 0.33, 0.40, 0.50, 0.58, 0.25, 0.16, 0.11, 0.13, 0.13, 0.14, 0.17, 0.20, 0.16, 0.15, 0.16, 0.16, 0.15, 0.16, 0.15, 0.16, 0.15, 0.14, 0.14, 0.15, 0.14, 0.14, 0.14, 0.14, 0.14, 0.15, 0.14, 0.14, 0.14, 0.14, 0.15, 0.14, 0.14, 0.15, 0.14, 0.14, 0.14, 0.14, 0.14, 0.13, 0.14, 0.13, 0.14, 0.13, 0.13, 0.14, 0.14, 0.14, 0.14, 0.14, 0.13, 0.13, 0.14, 0.14, 0.14, 0.13, 0.14, 0.13, 0.14, 0.13, 0.14, 0.14, 0.14, 0.14, 0.13, 0.13, 0.13, 0.14, 0.13, 0.14, 0.14, 0.13, 0.12, 0.13, 0.14, 0.14, 0.13, 0.13, 0.14, 0.14, 0.13, 0.12, 0.13, 0.13, 0.12, 0.12, 0.12, 0.13, 4.93, 17.39, 14.27, 14.51, 12.25, 10.03, 9.07, 7.58, 6.95, 19.88, 30.54, 37.53, 48.79, 49.17, 49.13, 49.11, 49.08, 49.06, 49.06, 49.04, 49.03, 49.01, 49.01, 49.00, 48.99, 48.98, 48.97, 48.97, 48.96, 48.96, 48.94, 48.95, 48.94, 48.92, 48.93, 48.92, 48.91, 48.91, 48.91, 48.91, 48.89, 48.89, 48.90, 48.89, 48.88, 48.88, 48.87, 48.87, 48.86, 48.86, 48.85, 48.85, 48.85, 48.85, 48.84, 48.84, 48.84, 48.83, 48.83, 48.82, 48.83, 48.82, 48.81, 48.81, 48.80, 48.81, 48.81, 48.83, 48.80, 48.79, 48.79, 48.78, 48.78, 48.78, 48.77, 48.78, 48.76, 48.77, 48.76, 48.75, 48.76, 48.75, 48.75, 48.75, 48.75, 48.74, 48.73, 48.74, 48.73, 48.74, 48.73, 48.73, 48.73, 0.14, 0.06, 0.08],\n"
            + "\t\t\t\t\t\t\tdownPre:[0.01, 2.50, 0.03, 0.04, 2.69, 2.67, 2.58, 2.10, 1.98, 1.74, 1.65, 9.40, 13.36, 17.28, 22.51, 27.94, 33.54, 39.30, 46.98, 47.83, 47.79, 47.76, 47.75, 47.73, 47.72, 47.71, 47.70, 47.69, 47.69, 47.68, 47.66, 47.66, 47.64, 47.65, 47.64, 47.63, 47.62, 47.62, 47.61, 47.61, 47.60, 47.61, 47.60, 47.59, 47.59, 47.58, 47.59, 47.57, 47.57, 47.57, 47.56, 47.56, 47.55, 47.55, 47.54, 47.54, 47.54, 47.53, 47.53, 47.52, 47.52, 47.51, 47.51, 47.51, 47.51, 47.50, 47.50, 47.50, 47.49, 47.49, 47.49, 47.48, 47.48, 47.49, 47.48, 47.48, 47.48, 47.47, 47.48, 47.47, 47.46, 47.45, 47.46, 47.45, 47.46, 47.46, 47.44, 47.45, 47.44, 47.44, 47.44, 47.45, 47.43, 47.44, 47.44, 47.43, 47.42, 47.43, 47.42, 0.06, 4.72, 4.99, 3.90, 4.47, 3.56, 2.03, 2.34, 2.07, 0.39, -0.01, 0.31, 0.35, 0.05, 0.06, 0.07, 0.06, 0.06, 0.05, 0.05, 0.05, 0.06, 0.06, 0.06, 0.06, 0.05, 0.05, 0.05, 0.05, 0.06, 0.06, 0.05, 0.06, 0.06, 0.06, 0.05, 0.05, 0.06, 0.06, 0.06, 0.06, 0.05, 0.05, 0.05, 0.06, 0.06, 0.05, 0.06, 0.06, 0.05, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.05, 0.05, 0.06, 0.06, 0.06, 0.06, 0.06, 0.05, 0.06, 0.06, 0.07, 0.05, 0.05, 0.06, 0.06, 0.07, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.05, 0.06, 0.05, 0.05, 0.05, 0.06, 0.06, 0.07, 0.06, 0.06, 0.05, 0.04, 0.04]\n"
            + "\t\t\t\t\t\t}\n" + "\t\t\t\t\t)\n" + "\t\t\t\t\twindow.onresize = preChart.resize\n"
            + "\t\t\t\t\tpreChart.setOption(options)\n" + "\t\t\t\t\tthis.shiGongHao = '数据测试'\n" + "\t\t\t\t},\n"
            + "\t\t\t\tmethods: {}\n" + "\t\t\t})\n" + "\n" + "\t\t\t\n" + "\t\t</script>\n" + "\t</body>\n" + "\n"
            + "</html>";
        createPDF(outputPath, content, "郑煤机");

    }
}


